package org.seed.rpc.core.routing;

import org.seed.rpc.core.RuntimeLocal;

import java.util.List;

public class AlwaysFirstStrategy implements RoutingStrategy {

    @Override
    public Endpoint determineEndpoint(String serviceName) {
        List<String> endpoints = RuntimeLocal.getEndpointList(serviceName);
        if (endpoints == null || endpoints.size() == 0) {
            return null;
        }
        return new Endpoint(endpoints.get(0));
    }

    @Override
    public Endpoint determineEndpointExcept(String serviceName, List<String> excepts) {
        List<String> endpoints = RuntimeLocal.getEndpointList(serviceName);
        if (endpoints == null || endpoints.size() == 0) {
            return null;
        }
        for (int i = 0; i < endpoints.size(); i++) {
            if (!excepts.contains(endpoints.get(i))) {
                return new Endpoint(endpoints.get(i));
            }
        }
        return null;
    }

}
